//
//  CRYShareManager.m
//  workWX
//
//  Created by 谢锷 on 2020/5/9.
//  Copyright © 2020 rongyiqiche Data and Technology Shanghai Co. Ltd. All rights reserved.
//

#import "CRYShareManager.h"
#import "WWKApi.h"
#import "CRYTool.h"
#import "work_wx_define.h"
#import "CRYWWApiManager.h"

@implementation CRYShareManager

+ (NSDictionary *) shareText:(NSString *) text {
    
    if (![WWKApi isAppInstalled]) {
        return [CRYWWApiManager isInstalled];
    }
    
    if ([CRYTool isContentNull:text]) {
        return [self getShareResultDic:CRYWorkWXErrorCode_Share_Text msg:CRYWorkWX_Text_Error result:NO];
    }
    WWKMessageTextAttachment *attachment = [[WWKMessageTextAttachment alloc] init];
    attachment.text         = text;
    return [self sendMsg:attachment type:CRYWorkWXShareType_Text];
}

+ (NSDictionary *) shareImage:(NSString *) imgName
                      imgPath:(NSString *) imgPath {
    
    if (![WWKApi isAppInstalled]) {
        return [CRYWWApiManager isInstalled];
    }
    
    if ([CRYTool isContentNull:imgName] || [CRYTool isContentNull:imgPath]) {
        return [self getShareResultDic:CRYWorkWXErrorCode_Share_Image msg:CRYWorkWX_Image_Error result:NO];
    }
    WWKMessageImageAttachment *attachment = [[WWKMessageImageAttachment alloc] init];
    return [self sendMsg:[self formatterFileShareParames:attachment filename:imgName path:imgPath] type:CRYWorkWXShareType_Image];
}

+ (NSDictionary *) shareVideo:(NSString *) videoName
                    videoPath:(NSString *) videoPath {
    
    if (![WWKApi isAppInstalled]) {
        return [CRYWWApiManager isInstalled];
    }
    
    if ([CRYTool isContentNull:videoName] || [CRYTool isContentNull:videoPath]) {
        return [self getShareResultDic:CRYWorkWXErrorCode_Share_Video msg:CRYWorkWX_Video_Error result:NO];
    }
    WWKMessageVideoAttachment *attachment = [[WWKMessageVideoAttachment alloc] init];
    return [self sendMsg:[self formatterFileShareParames:attachment filename:videoName path:videoPath] type:CRYWorkWXShareType_Video];
    
}

+ (NSDictionary *) shareFile:(NSString *) fileName
                    filePath:(NSString *) filePath {
    
    if (![WWKApi isAppInstalled]) {
        return [CRYWWApiManager isInstalled];
    }
    
    if ([CRYTool isContentNull:fileName] || [CRYTool isContentNull:filePath]) {
        return [self getShareResultDic:CRYWorkWXErrorCode_Share_File msg:CRYWorkWX_File_Error result:NO];
    }
    WWKMessageFileAttachment *attachment = [[WWKMessageFileAttachment alloc] init];
    return [self sendMsg:[self formatterFileShareParames:attachment filename:fileName path:filePath] type:CRYWorkWXShareType_Video];
    
}

+ (NSDictionary *) shareLink:(NSString *) title
                     summary:(NSString *) summary
                         url:(NSString *) url
                     iconUrl:(NSString *) iconUrl {
    
    if (![WWKApi isAppInstalled]) {
        return [CRYWWApiManager isInstalled];
    }
    
    if ([CRYTool isContentNull:title] || [CRYTool isContentNull:summary] || [CRYTool isContentNull:url] || [CRYTool isContentNull:iconUrl]) {
        return [self getShareResultDic:CRYWorkWXErrorCode_Share_Link msg:CRYWorkWX_Link_Error result:NO];
    }
    WWKMessageLinkAttachment *attachment = [WWKMessageLinkAttachment new];
    attachment.title        = title;
    attachment.summary      = summary;
    attachment.url          = url;
    attachment.iconurl      = iconUrl;
    
    if (![CRYTool isURLPathString:iconUrl]) {
        NSData *data = [NSData dataWithContentsOfFile:iconUrl];
        attachment.icon     = data;
    }
    return [self sendMsg:attachment type:CRYWorkWXShareType_Link];
}


+ (WWKMessageFileAttachment *) formatterFileShareParames:(WWKMessageFileAttachment *) attachment
                                                filename:(NSString *) filename
                                                    path:(NSString *) path {
    
    if (!attachment) {
        attachment = [[WWKMessageFileAttachment alloc] init];
    }
    
    attachment.filename     = filename;
    attachment.path         = path;
    if ([CRYTool isURLPathString:path]) {
        NSData *data = [NSData dataWithContentsOfURL:[NSURL URLWithString:path]];
        attachment.data     = data;
    }
    
    return attachment;
}

+ (NSDictionary *) sendMsg:(WWKMessageAttachment *) attachment
                      type:(CRYWorkWXShareType) type {
    
    WWKSendMessageReq *req  = [[WWKSendMessageReq alloc] init];
    req.attachment          = attachment;
    BOOL result = [WWKApi sendReq:req];
    return [self getShareResultDic:type msg:nil result:result];
}

+ (NSDictionary *) getShareResultDic:(NSInteger) type
                                 msg:(NSString *) msg
                              result:(BOOL) result {
    
    NSMutableDictionary *dic = [NSMutableDictionary dictionary];
    
    if (result) {
        [dic setValue:@(CRYWorkWXCode_Success) forKey:@"code"];
        [dic setValue:CRYWorkWX_Share_Suc forKey:@"msg"];
    } else {
        [dic setValue:@(type) forKey:@"code"];
        if ([CRYTool isContentNull:msg]) {
            msg = result ? CRYWorkWX_Share_Suc : CRYWorkWX_Share_Error;
        }
        [dic setValue:[CRYTool isContentNull:msg] ? @"" : msg forKey:@"msg"];
    }
    
    return dic;
}

@end
